package com.ruoyi.web.controller.biz;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.biz.domain.BizProject;
import com.ruoyi.biz.model.dto.BizProjectDto;
import com.ruoyi.biz.model.dto.ScoreDto;
import com.ruoyi.biz.model.dto.UpdateStatusDto;
import com.ruoyi.biz.model.req.BizProjectReq;
import com.ruoyi.biz.model.resp.BizProjectResp;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.service.BizProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 *
 * @author tianl
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/biz/bizProject")
public class BizProjectController extends BaseController {
    @Autowired
    private BizProjectService bizProjectService;

    @GetMapping(value = "/recalc")
    @Anonymous
    public AjaxResult recalc() {
        List<BizProject> projectList = bizProjectService.list();
        for (BizProject bizProject : projectList) {
            bizProjectService.calcProjectCost(bizProject.getId());
        }
        return success();
    }
    @PostMapping(value = "/updateScore")
    public AjaxResult updateScore(@RequestBody ScoreDto dto) {
        bizProjectService.score(dto);
        return success();
    }
    @GetMapping(value = "/listAll")
    public AjaxResult listAll(BizProjectReq bizProject) {
        bizProject.setValidProject(true);
        bizProject.setCreateTimeSort(true);
        return success(bizProjectService.listAll(bizProject));
    }

    @GetMapping("/list")
    public TableDataInfo list(BizProjectReq bizProject) {
        return getDataTable(bizProjectService.listPage(bizProject));
    }

    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProjectReq bizProject) {
        List<BizProjectResp> list = bizProjectService.list(bizProject);
        ExcelUtil<BizProjectResp> util = new ExcelUtil<>(BizProjectResp.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizProjectService.selectById(id));
    }

    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProjectDto bizProject) {
        bizProjectService.beforeSave(bizProject);
        return AjaxResult.success();
    }

    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProjectDto bizProject) {
        bizProjectService.beforeSave(bizProject);
        return AjaxResult.success();
    }

    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PostMapping("updateStatus")
    public AjaxResult updateStatus(@RequestBody UpdateStatusDto bizProject) {
        bizProjectService.updateStatus(bizProject);
        return AjaxResult.success();
    }

    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PostMapping("updateZg")
    public AjaxResult updateZg(@RequestBody BizProjectDto bizProject) {
        bizProjectService.updateZg(bizProject);
        return AjaxResult.success();
    }

    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizProjectService.deleteByIds(Arrays.asList(ids)));
    }
}
